package com.hs.pre.module.bardian;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hs.common.JSONHandle;
import com.hs.log.HsLogger;
import com.hs.pre.model.BardianBean;
import com.hs.pre.model.ProfileBean;
import com.hs.pre.model.UserBean;
import com.hs.pre.module.sort.SortDAO;
import com.hs.pre.page.PageParam;
import com.hs.pre.page.PageUtil;
import com.hs.pre.sys.validate.ParameterDic;



public class BardianAction extends org.apache.struts.actions.DispatchAction {
	private static final HsLogger logger = HsLogger
			.getLogger(BardianAction.class.getName());

	/**
	 * 个性设置列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			BardianDao bardianDao = new BardianDao();
			 PageParam pageParam = new PageParam(request);
			 /**
			  * 默认为15条
			  */
			 pageParam.setPageSize(15);
			 pageParam.setBasePath("/pre/bardian.do?bardian=list");
			 PageUtil pageUtil = bardianDao.getProfileList(pageParam);
			 request.setAttribute("pageUtil", pageUtil);
		return mapping.findForward("list");
	}

	/**
	 * 添加个性设置
	 * 个性设置内容格式为{tableName:'表名',field:[{serial:'主键',fieldId:'名称',fieldName:''},{serial:'主键',fieldId:'名称',fieldName:''}]}(json)
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
//			default
			String table = "default";
			String field = request.getParameter("fieldName");
			String viewNumTem = request.getParameter("viewNum");
	
			if(field != null &&! (field=field.trim()).equals(""))
			{
				Integer viewNum = new Integer(10); 
				if(viewNumTem !=null &&! (viewNumTem=viewNumTem.trim()).equals(""))
					viewNum = new Integer(viewNumTem.trim());
				String tableTem = request.getParameter("tableName");
				if(tableTem != null && !(tableTem = tableTem.trim()).equals(""))
					table = tableTem;
				List list = new ArrayList();
				String[] values = field.split("@");
				for(int i=0 ;i<values.length;i++)
				{
					String[] tem = values[i].split(":");
					BardianBean bardianBean = new BardianBean();
					bardianBean.setSerial(new Integer(i));
					bardianBean.setFieldId(tem[0]);
					bardianBean.setFieldName(tem[1]);
					list.add(bardianBean);
				}
				JSONHandle jSONHandle = new JSONHandle();
				String configStr = "{tableName:'"+table+"',field:"+jSONHandle.getJsonToStr(list)+"}";
				UserBean userBean = (UserBean) request.getSession().getAttribute(
						ParameterDic.USERSESSION);
				if(userBean != null){
					ProfileBean profileBean = new ProfileBean();
					profileBean.setLoginId(userBean.getUserId()+"");
					profileBean.setConfigStr(configStr);
					profileBean.setIsUse(Parameter.ISUSEY);
					profileBean.setViewNum(viewNum);
					BardianDao bardianDao = new BardianDao();
					bardianDao.addProfile(profileBean);
				}
				
			}
			
		return new ActionForward(mapping.findForward("index").getPath(),true);
	}
	
	
	/**
	 * 删除个性设置
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String profileIdTem = request.getParameter("profileId");
		if(profileIdTem != null && !(profileIdTem = profileIdTem.trim()).equals(""))
		{
			Long profileId = new Long(profileIdTem);
			BardianDao bardianDao = new BardianDao();
			bardianDao.delProfile(profileId);
		}
		return new ActionForward(mapping.findForward("index").getPath(),true);
	}
	
	/**
	 * 更新个性设置
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String profileIdTem = request.getParameter("profileId");
		if(profileIdTem != null && !(profileIdTem = profileIdTem.trim()).equals(""))
		{
			UserBean userBean = (UserBean) request.getSession().getAttribute(
					ParameterDic.USERSESSION);
			if(userBean != null){
				Long profileId = new Long(profileIdTem);
				BardianDao bardianDao = new BardianDao();
				bardianDao.updateProfile(profileId, userBean.getUserId()+"");
			}
			
		}
		return mapping.findForward("index");
	}
}
